<template>
  <div class="app-container">
    <div class="app-container-flex">
      <NewFullScreen v-model="showDialog" title="耗材追溯" :needReturn="false">
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <PageFilter :filter-list="filterInfo.list">
              <template v-slot:filter-scanCode>
                <ScanInputBox
                  v-model:query="scanInfo.query"
                  :api="scanInfo.api"
                  @onCallBackScanData="onCallBackScanData"
                />
              </template>
            </PageFilter>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>产品信息</p>
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <PageTable
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :summary="true"
                :check-box="false"
                :tab-index="true"
                :summaryFieldList="tableInfo.summaryFieldList"
                :field-list="tableInfo.fieldList"
                :handle="tableInfo.handle"
              >
              </PageTable>
            </div>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>流程信息</p>
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <PageTable
                :refresh="affairTableInfo.refresh"
                :init-curpage="affairTableInfo.initCurpage"
                v-model:data="affairTableInfo.data"
                :pager="false"
                :check-box="false"
                :tab-index="true"
                :field-list="affairTableInfo.fieldList"
                :handle="affairTableInfo.handle"
                :span-method="SpanMethod"
              />
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <ChosePrinter :visible="printerInfo.visible" @callBack="printerCallBack" />
  </div>
</template>
<script setup name="productTracingTheSource">
import {
  productSourceAll,
  productMsgPage,
  productSourcePrint,
} from '@/api/dataQuery.js'

import { getCustomizeSetting } from '@/api/customize'
import ScanInputBox from './ScanInputBox'
const { proxy } = getCurrentInstance()
const showDialog = ref(true)
const spanArr = ref([])
const factoryMainCode = ref('')
const factorySubCode = ref('')
const qrCode = ref('')
const scanInfo = ref({
  api: productMsgPage,
  query: {},
})
const filterInfo = ref({
  query: {
    code: '',
  },
  list: [{ label: '产品条码', value: 'scanCode', type: 'slot' }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  summaryFieldList: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '打印二维码',
        event: printSingleQrCode,
        show: proxy.$permission('otherStoreOrder:printCode'),
      },
    ],
  },
})
const affairTableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '单号', value: 'orderNo' },
    { label: '单据类别', value: 'sequenceName' },
    { label: '操作人', value: 'operateUserName' },
    { label: '入库部门', value: 'storeDeptName' },
    { label: '入库仓库', value: 'storeDepotName' },
    { label: '出库部门', value: 'exportDeptName' },
    { label: '出库仓库', value: 'exportDepotName' },
    { label: '出入库数量', value: 'exportStoreQty' },
    { label: '单据操作种类', value: 'operateTypeValue' },
    { label: '患者就诊号', value: 'patientNo' },
    { label: '患者', value: 'patientName' },
    { label: '医生', value: 'doctorName' },
    { label: '操作时间', value: 'operateTime' },
  ],
})
const printerInfo = ref({
  visible: false,
  data: {},
})
//合并单元格
function SpanMethod(data) {
  let { rowIndex, column } = data
  if (
    column.property === 'orderNo' ||
    column.property === 'sequenceName' ||
    column.property === 'operateUserName' ||
    column.property === 'storeDeptName' ||
    column.property === 'storeDepotName' ||
    column.property === 'exportDeptName' ||
    column.property === 'exportDepotName' ||
    column.property === 'exportStoreQty'
  ) {
    let obj = [0, 0]
    spanArr.value.some((v) => {
      if (rowIndex === v.row) {
        obj = [v.index, 1]
      }
    })
    return obj
  }
}
//回调
function onCallBackScanData(data) {
  if (data && data.result && data.result.length > 0) {
    tableInfo.value.data = data.result
    const params = data.searchParams
    if (data.result.length) {
      factoryMainCode.value = params.factoryMainCode
      factorySubCode.value = params.factorySubCode
      qrCode.value = params.code
      getProductSource()
    }
  } else {
    affairTableInfo.value.data = []
    tableInfo.value.data = []
    proxy.$message({
      message: `系统中没有当前码的有效信息`,
      type: 'error',
      duration: 3500,
      showClose: true,
    })
  }
}
//打印单个二维码
function printSingleQrCode(row = undefined) {
  if (!row.qrCode) {
    proxy.$message({
      message: '没有生成二维码，不能打印',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  productSourcePrint(row).then((resp) => {
    if (resp && resp.success) {
      printerInfo.value.visible = true
      printerInfo.value.data = resp.data
    }
  })
}
//获取产品流程信息
function getProductSource() {
  let params = []
  params = {
    code: qrCode.value,
    factoryMainCode: factoryMainCode.value,
    factorySubCode: factorySubCode.value,
  }
  productSourceAll(proxy.$fn.delEmptyAttribute(params)).then((res) => {
    if (res && res.success) {
      let arr = []
      let n = 0
      affairTableInfo.value.data = []
      res.data.forEach((item, i, data) => {
        if (arr.length) {
          n = arr[arr.length - 1].row + data[i - 1].orderAffairVOS.length
        }
        arr.push({
          row: n,
          index: item.orderAffairVOS.length,
        })
        if (item.orderAffairVOS && item.orderAffairVOS.length) {
          item.orderAffairVOS.forEach((e) => {
            affairTableInfo.value.data.push({
              orderNo: item.orderNo,
              sequenceName: item.sequenceName,
              operateUserName: item.operateUserName,
              storeDeptName: item.storeDeptName,
              storeDepotName: item.storeDepotName,
              exportDeptName: item.exportDeptName,
              exportDepotName: item.exportDepotName,
              exportStoreQty: item.exportStoreQty,
              operateTime: e.operateTime,
              operateTypeValue: e.operateTypeValue,
              patientNo: e.patientNo,
              patientName: e.patientName,
              doctorName: e.doctorName
            })
          })
        }
      })
      spanArr.value = arr
    }
  })
  qrCode.value = undefined
  factoryMainCode.value = undefined
  factorySubCode.value = undefined
}
function printerCallBack(printer) {
  if (printer !== false) {
    proxy.$print.buildRenderBarCode(printerInfo.value.data, printer)
  }
  printerInfo.value.visible = false
}
function getCustomize() {
  let param = {
    tableKey:
      'productSourceDetail-com.slip.spd.entity.vo.dataquery.ProductSourceDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
    }
  })
}

let obj = {
  price: {
    label: '单价',
    value: 'price',
    minWidth: '150px',
    type: 'column',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('productTracingTheSource:price'),
    hidden: true,
  },
  sumAmount: {
    label: '总金额',
    value: 'sumAmount',
    type: 'column',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('productTracingTheSource:price'),
    hidden: true,
  },
  orderNo: {
    label: '单号',
    value: 'orderNo',
    componentType: 'input',
    show: false,
    type: 'column',
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()
</script>
